package csc.traning.wpsj.servlet;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.commons.validator.routines.EmailValidator;

import csc.traning.wpsj.dao.User;
import csc.traning.wpsj.dao.UserDB;

/**
 * Servlet implementation class EditUserProfile
 */
public class EditUserProfile extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public EditUserProfile() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		request.setAttribute("err", "");
		HttpSession session = request.getSession();
		String firstName = request.getParameter("firstName");
		String lastName = request.getParameter("lastName");
		String userName = request.getParameter("username");
		String email = request.getParameter("email");
		String currentPass = request.getParameter("crrPass");
		String newPass = request.getParameter("newPass");
		String cNewPass = request.getParameter("cNewPass");
		String phone = request.getParameter("phone");
		boolean isValid = true;
		
		if("".equals(firstName)){
			isValid = false;
			request.setAttribute("errF", "First name must not be empty!");
		}
		if("".equals(lastName)){
			isValid = false;
			request.setAttribute("errL", "Last name must not be empty!");
		}
		if("".equals(email)){
			isValid = false;
			request.setAttribute("errE", "Email must not be empty!");
		} else {
			if(!EmailValidator.getInstance().isValid(email)){
				isValid = false;
				request.setAttribute("errE", "Email is not valid!");
			}
		}
		if("".equals(currentPass)){
			isValid = false;
			request.setAttribute("errP", "Current password must not be empty!");
		}
		if(!"".equals(newPass)){
			if("".equals(cNewPass) || !newPass.equals(cNewPass)){
				isValid = false;
				request.setAttribute("errN", "New password and confirm new password must be matched togeter!");
			}
		}
		if(!"".equals(phone)){
			if(!phone.matches("\\d{10}")){
				isValid = false;
				request.setAttribute("errPN", "Phone number is not valid!");
			}
		}

		if (!isValid) {
			request.getRequestDispatcher("profile_editor.jsp").forward(request,
					response);
		} else {

			User user = UserDB.getUser(userName, currentPass);
			if (user != null) {
				int index = UserDB.getUserList().indexOf(user);
				user.setCellphone(phone);
				user.setEmailAddress(email);
				user.setFirstName(firstName);
				user.setLastName(lastName);
				user.setCellphone(phone);
				if (!"".equals(newPass) && !newPass.equals(user.getPassword())) {
					user.setPassword(newPass);
				}

				// update User
				UserDB.getUserList().set(index, user);
				session.setAttribute("user", user);

				request.setAttribute("err", "Update your profile successfully!");
			} else {
				request.setAttribute(
						"errP",
						"Your current password is not correct!");
			}
			request.getRequestDispatcher("profile_editor.jsp").forward(request,
					response);
		}
	}

}
